<?php

namespace Web\MainSiteBundle\Controller;

use Model\CourseBundle\Entity\Course;
use Model\CourseBundle\Entity\CourseLessonArrange;
use Model\CourseBundle\Model\CourseLessonArrangeModel;
use Model\CourseBundle\Model\CourseLessonContentModel;
use Model\CourseBundle\Model\CourseModel;
use Model\CourseCategoryBundle\Model\CourseCategoryModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;

/**
 * @route("course")
 */
class ManageCourseLessonArrangeController extends WebBaseController
{
    /**
     * @route("/courseLessonArrange/add/{uniqueId}",name="WebCourseLessonArrangeAdd")
     */
    public function editCourseLessonArrangeAdd(Request $request , Course $course)
    {//添加课程目录
        $chk = $this->needToBeCourseManager($course);
        $courseId = $course->getId();
        $courseLessonArrangeModel = new CourseLessonArrangeModel($this->getDoctrine());
        if ($request->getMethod() == "POST") {
            if (!$parent = $this->getParamRequired("parent")) {$parent = 0;}
            if (!$title = $this->getParamRequired("title")) {$this->showMessagePage("章节标题不能为空");}
            if (!$orders = $this->getParamRequired("orders")) {$orders=0;}
            if (!$info = $this->getParamRequired("info")) {$this->showMessagePage("请填写章节简介");}
            $params = array(
                "course"  => $courseId,
                "name"  => $title,
                "parent"=> $parent,
                "orders"=> $orders,
                "info"  => $info,
            );
            if ($id = $this->getParamRequired("id")) {
                $params["id"] = $id;
            }
            $rst = $courseLessonArrangeModel->commWebAdd($params);
            if (! $rst) {
                $this->showMessagePage("系统错误请刷新后重试，如果错误仍然存在，请与管理员联系！");
                return "unreachable!";
            } else {
                return $this->redirectToRoute("WebCourseLessonList",array("uniqueId"=>$course->getUniqueId()));
            }
        } else {
            $id = $this->getParamRequired("id");
            $classes = $courseLessonArrangeModel->getAllCourseLessonArrangeTree($courseId);
            $data["courseLessonArrange"] = $classes;
            $data["course"] = $course;
            if ($id) {//编辑
                $entity = $courseLessonArrangeModel->getOneArray($id,$courseId);
                $data["entity"] = $entity;
            }
            return $this->render("WebMainSiteBundle:Course:course_lesson_arrange_add.html.twig",$data);
        }
    }

    /**
     * @route("/courseLessonArrange/list/{uniqueId}",name="WebCourseLessonArrangeList")
     */
    public function editCourseLessonArrangeList(Request $request , Course $course)
    {//获取课程章节目录
        $this->needToBeCourseManager($course);
        $courseId = $course->getId();
        $courseLessonArrange = new CourseLessonArrangeModel($this->getDoctrine());
        $all = $courseLessonArrange->getAllCourseLessonArrangeTree($courseId);
        $data = array(
            "course" => $course,
            "courseLessonArrange" => $all,
        );
        return $this->render("WebMainSiteBundle:Course:course_lesson_arrange_list.html.twig",$data);
    }
}
